$(function(){
	getParams();
});

function getParams(){
	var id = $.query.get("id");
	var newsTypeId = $.query.get("newsTypeId");
	if(id){
		var newsId = Base64.decode(id);
		var newsType = Base64.decode(newsTypeId);
		$("#newsTypeId").val(newsType);
		getNewsInfo(newsId,newsType);
	} else {
		getNewsList();
	}
}

function getNewsList(){
	var newsTypeId = $("#newsTypeId").val();
	$.ajax({
		url : '/api/news/list',
		data : {
			newsTypeId : newsTypeId
		},
		type : 'post',
		success : function(resp){
			if(resp.success){
				$("#dataList").empty();
				$.each(resp.data,function(i,data){
					var str = "<a href=\"newsDetail.html?id=" +Base64.encode(data.id.toString()) + "&newsTypeId=" + Base64.encode(newsTypeId.toString()) + "\"><li class=\"clearfix\">";
					str += '<div class="news-pic"><img style="width:200px;height:100px;" src="' + data.path + '" alt=""></div>';
					str += "<div class=\"news-desc\">";
					str += "<h2 class=\"title\">" + data.title + "</h2>";
					str += "<span class=\"time\">" + data.createTime + "</span>";
					str += "</div>";
					str += "</li></a>";
					$("#dataList").append(str);
				});
			}else if(resp.code == 1006){
				window.location.href="login.html";
			}else{
				art.dialog({
					icon : 'error',
					time : 2,
					content : resp.msg
				});
			}
		}
	});
}

function getNewsInfo(id,newsTypeId){
	var datas = {
		id : parseInt(id),
		newsTypeId : newsTypeId
	}
	$.ajax({
		url : '/api/news/info',
		data : datas,
		type : 'post',
		success : function(resp){
			if(resp.success){
				var info = resp.data.info;
				if(info == undefined){
					return;
				}
				var newsTypeId = $("#newsTypeId").val();
				newsTypeId = parseInt(newsTypeId);
				if(newsTypeId == 1){
					$("#newsType").text('行业新闻/');	
					$(".nav-left ul li").removeClass('active');
					$(".nav-left ul li").eq(0).addClass('active');
				} else if(newsTypeId == 2){
					$("#newsType").text('公司动态/');
					$(".nav-left ul li").removeClass('active');
					$(".nav-left ul li").eq(1).addClass('active');
				} else if(newsTypeId == 3){
					$("#newsType").text('政策法规/');
					$(".nav-left ul li").removeClass('active');
					$(".nav-left ul li").eq(2).addClass('active');
				}
				$("#title").text(info.title);
				$("#createTime").text(info.createTime);
				$("#keyWord").text("关键词：" + (info.keyWord == null ? "无":info.keyWord));
				$("#content").html(info.content);
				$("#img_path").append('<img style="width:480px;height:260px;" src="' + info.path + '" alt="">');
				var before = resp.data.before;
				var after = resp.data.after;
				$("#before").html(before == undefined ? '' : ('上一篇：<a href="newsDetail.html?id= ' + Base64.encode(before.id.toString()) + '&newsTypeId=' + Base64.encode(newsTypeId.toString()) + '">' + before.title + '</a>'));
				$("#after").html(after == undefined ? '' : ('下一篇：<a href="newsDetail.html?id=' + Base64.encode(after.id.toString()) + '&newsTypeId=' + Base64.encode(newsTypeId.toString()) + '">' + after.title + '</a>'));
			}else if(resp.code == 1006){
				window.location.href="login.html";
			}else{
				art.dialog({
					icon : 'error',
					time : 2,
					content : resp.msg
				});
			}
		}
	});
}

function UrlDecode(str){ 
	  var ret=""; 
	  for(var i=0;i<str.length;i++){ 
	   var chr = str.charAt(i); 
	    if(chr == "+"){ 
	      ret+=" "; 
	    }else if(chr=="%"){ 
	     var asc = str.substring(i+1,i+3); 
	     if(parseInt("0x"+asc)>0x7f){ 
	      ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6))); 
	      i+=5; 
	     }else{ 
	      ret+=asc2str(parseInt("0x"+asc)); 
	      i+=2; 
	     } 
	    }else{ 
	      ret+= chr; 
	    } 
	  } 
	  return ret; 
} 
